package xiaopiya.robot.handler.weather;

import java.io.Serializable;

/**
 * Tq121天气对象
 * @author Ray (ayufox@gmail.com)
 * @version 0.1
 */
public class Tq121Weather implements Serializable
{
	public String date;

	public String weatherInfo;

	public String temperature;

	public String wind;

	public String light;

	public Tq121Weather(String weatherText)
	{
		parse(weatherText);
	}

	//解析指定城市的天气
	private void parse(String weatherText)
	{
		// 解析时间
		this.parseDate(weatherText);
		// 解析天气
		this.parseWeather(weatherText);
		// 解析温度
		this.parseTemperature(weatherText);
		// 解析风，光
		this.parseWindAndLight(weatherText);
	}

	// 解析时间
	private void parseDate(String weatherText)
	{
		this.date = this.getStringBetween(weatherText,
				"<span class=\"big-cn\">", "</span>").trim();
	}

	// 解析天气
	private void parseWeather(String cityWeatherPageText)
	{
		this.weatherInfo = this.getStringBetween(cityWeatherPageText,
				"class=\"weather\">", "</td>").trim();
	}

	// 解析温度
	private void parseTemperature(String cityWeatherPageText)
	{
		this.temperature = this.getStringBetween(cityWeatherPageText,
				"class=\"weatheren\">", "</td>").trim();
	}

	// 解析风，光
	private void parseWindAndLight(String cityWeatherPageText)
	{
		String windAndLight = this.getStringBetween(cityWeatherPageText,
				"<td width=\"153\" valign=\"top\"><span class=\"big-cn\">",
				"</td>");
		this.wind = this.getStringBetween(windAndLight, "", "<br>").trim();
		this.light = this.getStringBetween(windAndLight, "<br>", "</span>")
				.trim();
	}

	private String getStringBetween(String sourceString, String startString,
			String endString)
	{
		try
		{
			int startPos = sourceString.indexOf(startString)
					+ startString.length();
			int endPos = sourceString.indexOf(endString, startPos);
			String result = sourceString.substring(startPos, endPos);
			return result;
		} catch (Throwable t)
		{
			return null;
		}
	}

	public String toString()
	{
		String LN = "\r\n";
		String result = "日期：" + this.date + LN;
		result += "天气：" + this.weatherInfo + LN;
		result += "温度：" + this.temperature + LN;
		result += "风：" + this.wind + LN;
		result += "" + this.light + LN;
		return result;
	}
}
